package cn.iocoder.yudao.module.team.service.fleet;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.team.controller.admin.fleet.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.fleet.FleetDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.team.dal.mysql.fleet.FleetMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.team.enums.ErrorCodeConstants.*;

/**
 * 团队管理-车队信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FleetServiceImpl implements FleetService {

    @Resource
    private FleetMapper fleetMapper;

    @Override
    public Integer createFleet(FleetSaveReqVO createReqVO) {
        // 插入
        FleetDO fleet = BeanUtils.toBean(createReqVO, FleetDO.class);
        fleetMapper.insert(fleet);

        // 返回
        return fleet.getId();
    }

    @Override
    public void updateFleet(FleetSaveReqVO updateReqVO) {
        // 校验存在
        validateFleetExists(updateReqVO.getId());
        // 更新
        FleetDO updateObj = BeanUtils.toBean(updateReqVO, FleetDO.class);
        fleetMapper.updateById(updateObj);
    }

    @Override
    public void deleteFleet(Integer id) {
        // 校验存在
        validateFleetExists(id);
        // 删除
        fleetMapper.deleteById(id);
    }

    @Override
        public void deleteFleetListByIds(List<Integer> ids) {
        // 删除
        fleetMapper.deleteByIds(ids);
        }


    private void validateFleetExists(Integer id) {
        if (fleetMapper.selectById(id) == null) {
            throw exception(FLEET_NOT_EXISTS);
        }
    }

    @Override
    public FleetDO getFleet(Integer id) {
        return fleetMapper.selectById(id);
    }

    @Override
    public PageResult<FleetDO> getFleetPage(FleetPageReqVO pageReqVO) {
        return fleetMapper.selectPage(pageReqVO);
    }

    @Override
    public void createBatch(List<FleetDO>  createReqVO) {
        fleetMapper.insertOrUpdate(createReqVO);
    }

}